package com.rainng.coursesystem.controller;

import com.rainng.coursesystem.manager.CourseNotificationManager;
import com.rainng.coursesystem.model.CourseNotificationItem;
import com.rainng.coursesystem.service.CourseNotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("course-notifications")
public class CourseNotificationController {

    @Autowired
    private CourseNotificationManager courseNotificationManager;

    @GetMapping
    public ResponseEntity<List<CourseNotificationItem>> getNotificationsByCourseId(@RequestParam Long courseId) {
        List<CourseNotificationItem> notifications = courseNotificationManager.getNotificationsByCourseId(courseId);
        return ResponseEntity.ok(notifications);
    }

    @PostMapping
    public ResponseEntity<CourseNotificationItem> createNotification(@RequestBody CourseNotificationItem notification) {
        notification.setCreatedAt(LocalDateTime.now());  // 确保设置创建时间
        CourseNotificationItem createdNotification = courseNotificationManager.createNotification(notification);
        return new ResponseEntity<>(createdNotification, HttpStatus.CREATED);
    }

    @DeleteMapping
    public ResponseEntity<Void> deleteNotification(@RequestParam Long id) {
        boolean deleted = courseNotificationManager.deleteNotification(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

}